package com.all.play.myadapterdrag.callback;

import android.content.Context;
import android.graphics.Canvas;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import com.all.play.myadapterdrag.R;
import com.all.play.myadapterdrag.interfaces.ItemMoveListener;

/**
 * Created by 聪明一只哈 on 2018/5/22.
 * Time: 2018/5/22  15:47
 */

public class MyItemTouchHelperCallBack extends ItemTouchHelper.Callback {


    private Context context;
    private ItemMoveListener itemMoveListener;

    public MyItemTouchHelperCallBack(Context context, ItemMoveListener itemMoveListener) {
        this.context = context;

        this.itemMoveListener = itemMoveListener;
    }

    /**
     * CallBack回调监听时先调用,用来判断当前是什么动作
     * 比如,判断方向(进行那个方向的拖动)
     *
     * @param recyclerView
     * @param viewHolder
     * @return 方向的int值
     */
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        /**
         * 方向:up,down,left,right
         */
        int up = ItemTouchHelper.UP;
        int down = ItemTouchHelper.DOWN;
        //监听上下拖拽
        int dragFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        //监听侧滑
        int swipeFlag = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        //得出两种方向的最终结果
        int flags = makeMovementFlags(dragFlag, swipeFlag);


        return flags;
    }


    /**
     * 是否打开长按拖拽
     *
     * @return
     */
    @Override
    public boolean isLongPressDragEnabled() {
        return true;
    }

//    /**
//     * 是否打开左右滑动,貌似注释不正确
//     * @return
//     */
//    @Override
//    public boolean isItemViewSwipeEnabled() {
//        return super.isItemViewSwipeEnabled();
//    }

    /**
     * 条目移动的时候,被触发
     *
     * @param recyclerView
     * @param srcholder
     * @param tarholder
     * @return
     */
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder srcholder,
                          RecyclerView.ViewHolder tarholder) {
        //必须是不同的条目
        if (srcholder.getItemViewType() != tarholder.getItemViewType()) {
            return false;
        }
        //在拖拽的过程当中,不断调用notifyitemMove()

        itemMoveListener.onItemMove(srcholder.getAdapterPosition(),
                tarholder.getAdapterPosition());
        return true;
    }

    /**
     * 条目侧滑的时候,被触发
     *
     * @param viewHolder
     * @param direction  方向
     */
    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        //监听侧滑,调用notifyItemRemove(position)
        //1.删除数据
        //2.刷新
        //使用接口回调
        itemMoveListener.onItemRemove(viewHolder.getAdapterPosition());
    }


    /**
     * @param viewHolder
     * @param actionState 动作的状态
     */
    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {

        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
            //未恢复原来的颜色-->clearView()
            viewHolder.itemView.setBackgroundColor(ContextCompat.getColor(context, R.color.backgroundcolor));

        }
        super.onSelectedChanged(viewHolder, actionState);
    }

    /**
     * 恢复原来的颜色
     *
     * @param recyclerView
     * @param viewHolder
     */
    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        //恢复原来的颜色
        viewHolder.itemView.setBackgroundColor(ContextCompat.getColor(context, R.color.background));
        //第一种方式
//        viewHolder.itemView.setAlpha(1);
//        viewHolder.itemView.setScaleX(1);
//        viewHolder.itemView.setScaleY(1);
        super.clearView(recyclerView, viewHolder);
    }


    /**
     * 重绘child,可以依靠此方法添加特效
     *
     * @param c
     * @param recyclerView
     * @param viewHolder
     * @param dX                水平方向移动的增量 +:向右  -:向左  范围:0~view.getWidth()
     * @param dY
     * @param actionState
     * @param isCurrentlyActive
     */
    @Override
    public void onChildDraw(Canvas c,
                            RecyclerView recyclerView,
                            RecyclerView.ViewHolder viewHolder,
                            float dX, float dY, int actionState, boolean isCurrentlyActive) {

        float alpha = 1 - Math.abs(dX) / viewHolder.itemView.getWidth();
        //根据移动方向设置动画特效
        if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
            //透明动画

            viewHolder.itemView.setAlpha(alpha);//1~0
            viewHolder.itemView.setScaleX(alpha);
            viewHolder.itemView.setScaleY(alpha);
            //---这里设为透明,那么在itemview复用的时候,被服用的条目还是
            //透明色,需要在clearView()重新设为非透明色
        }

        //方式2
        if (alpha == 0) {
            viewHolder.itemView.setAlpha(1);//1~0
            viewHolder.itemView.setScaleX(1);
            viewHolder.itemView.setScaleY(1);
        }

        //TODO  这是思路1
        //滑动出现删除按钮
        //判断是否超出with/2,就让setTranslation()停在一半的位置
//        if (Math.abs(dX) <= viewHolder.itemView.getWidth() / 2) {
//            viewHolder.itemView.setTranslationX(-0.5f * viewHolder.itemView.getWidth());
//        } else {
//            viewHolder.itemView.setTranslationX(dX);
//        }
        //TODO  思路2 使用viewpager



        //默认super也是有滑动到一半时,的特效处理
        // super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }
}
